<template>
    <div>
        <table border="1">
            <tbody>
                <tr>
                    <th>姓名</th>
                    <th>
                        <input type="text" v-model="data.Name">
                    </th>
                </tr>
                <tr>
                    <th>性别</th>
                    <th>
                        <input type="radio" v-model="data.Sex" name="Sex" value="男">男
                        <input type="radio" v-model="data.Sex" name="Sex" value="女">女
                    </th>
                </tr>
                <tr>
                    <th>房间号</th>
                    <th>
                        <input type="text" v-model="data.Num">
                    </th>
                </tr>
                <tr>
                    <th>房价类型</th>
                    <th>
                        <select v-model="data.LevelId">
                            <option value="1">大床房</option>
                            <option value="2">双人床</option>
                            <option value="3">豪华套间</option>
                            <option value="4">总统套房</option>
                            <option value="5">单人间</option>
                        </select>
                    </th>
                </tr>
                <tr>
                    <th>爱好</th>
                    <th>
                        <span v-for="h in hobbys">
                            <input v-model="hb" type="checkbox" :value="h" >{{ h }}
                        </span>
                    </th>
                </tr>
                <tr>
                    <th>图片</th>
                    <th>
                        <input type="file"  @change="Up">
                        <img :src="'https://localhost:7212/'+data.Img" style="width: 100px; height: 100px;"   >
                    </th>
                </tr>
                <tr>
                    <th>
                        <input type="button" value="添加" @click="Add">
                    </th>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref } from 'vue';
import { onMounted } from 'vue';

const hb=ref([])

const hobbys=ref(["游泳","登山","小游戏","篮球"])


const data=ref({
    "Id": 0,
  "Name": "",
  "Sex": "",
  "Num": "",
  "LevelId": 0,
  "Hobby": "",
  "HobbyCount": 0,
  "Img": "string",
  "Delete": true
})


const Up=(e:any)=>{
    var f=e.target.files[0];
    var fd=new FormData();

    fd.append("file",f);

    if(f.size>1024*1024*2){
        alert('图片不能超过1mb')
        return;
    }
    if(f.type!="image/jpeg" && f.type!="image/png"){
        alert('格式不正确');
        return;
    }

    axios.post("https://localhost:7212/api/UpLoad/Up",fd).then(res=>{
        data.value.Img=res.data;
    })

}

const Add=()=>{
    data.value.Hobby=hb.value.toString();
    data.value.HobbyCount=hb.value.length;

    axios.post("https://localhost:7212/api/Home/AddHome",data.value).then(res=>{
        if(res.data>0){
            alert('添加成功')
        }
        else{
            alert('失败')
        }
    })
}


</script>

